package com.cn.aam.checaiduo.ui.more;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.utils.WindowsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


@Route(path = UIRouter.MORE.JUST_SHOW_IMAGE, name = "显示图片-用户显示长图")
public class JustShowImageActivity extends BaseActivity {
    public final static String DATA_IMG_DISPATCH = "data_img_dispatch";

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private JustImageDataAdapter mAdapter;


    private ImgDispatch imgDispatch;



    @Override
    protected int layoutId() {
        return R.layout.activity_just_show_image;
    }

    @Override
    protected void onGenerate() {
        imgDispatch = (ImgDispatch) getIntent().getSerializableExtra(DATA_IMG_DISPATCH);
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle(imgDispatch.getTitle())
                .setTitleColor(R.color.black_normal);
        //recy
        mAdapter = new JustImageDataAdapter(mContext);
        recyclerview.setLayoutManager(new GridLayoutManager(mContext, 1));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.setAdapter(mAdapter);


        List<Integer> list = new ArrayList<>();
        list.add(imgDispatch.getRes());
        mAdapter.setDataToAdapter(list);
    }


    public enum ImgDispatch {
        Reserve("如何预约投资", R.drawable.img_reserve_invest),
        Procedure("投资操作流程", R.drawable.img_invest_prodcedure),
        ChangeMobile("如何更换手机号?", R.drawable.img_change_mobile),
        ChangeBank("如何更换银行卡？", R.drawable.img_change_bank),
        WithDraw("提现须知", R.drawable.img_with_draw);

        private String title;
        private Integer Res;

        ImgDispatch(String title, Integer res) {
            this.title = title;
            Res = res;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public Integer getRes() {
            return Res;
        }

        public void setRes(Integer res) {
            Res = res;
        }
    }


    /**
     * 适配器
     */
    class JustImageDataAdapter extends BaseRecycerAdapter<Integer> {


        JustImageDataAdapter(Context mContext) {
            super(mContext);
        }

        //item
        @Override
        protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
            return new ItemViewHolder(picNewsView);
        }

        @Override
        protected int getItemLayoutRes() {
            return R.layout.list_item_just_img;
        }


        @Override
        protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {
            final ItemViewHolder myholdet = (ItemViewHolder) recyclerholder;
            Integer item = getItem(position);

            //在不用加载bimap的情况下，获取图片资源的长宽
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;//这个参数设置为true才有效，
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item, options);
//            if (bitmap == null) {
//                Logger.e("bitmap 为空");
//            }
            int bitmapheight = options.outHeight;
            int bitmapWidth = options.outWidth;
            int screenWidth = WindowsUtils.getScreenWidth(mContext);
            int imageViewHieght = screenWidth * bitmapheight / bitmapWidth;
//            Logger.e("高度=" +bitmapheight + " 宽度=" + bitmapWidth + " 长宽比=" + bitmapheight + "/" + bitmapWidth);
            myholdet.ivProductData.setMinimumHeight(imageViewHieght);
            Glide.with(mContext).load(item).crossFade().into(myholdet.ivProductData);
        }


        //Item
        class ItemViewHolder extends BaseRecyclerViewHolder {
            @BindView(R.id.iv_product_data)
            ImageView ivProductData;

            ItemViewHolder(View view) {
                super(view);
            }
        }


    }


}
